package com.gyh.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gyh.entity.Member;
import com.gyh.entity.Reserve;
import com.gyh.service.MemberService;
import com.gyh.service.ReserveService;
import com.gyh.util.JWTUtil;
import com.gyh.util.Result;
import com.gyh.vo.ReserveVo;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/reserve")
public class ReserveController {
    @Resource
    private ReserveService reserveService;
    @Resource
    private MemberService memberService;
    @GetMapping
    public Result add(Integer courseId,HttpServletRequest request){
        String token = request.getHeader("token");
        String phone = null;
        try {
            Claims claims = JWTUtil.parseJWT(token);
            phone = claims.getSubject();
        } catch (Exception e) {
            return Result.error(401,"登录失效");
        }
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        Member member = memberService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(member)){
            return Result.error(401,"登录失效");
        }
        Reserve reserve = new Reserve();
        reserve.setCourseId(courseId);
        reserve.setMemberId(member.getId());
        try {
            int i = reserveService.add(reserve);
            if (i<1){
                return Result.error(401,"添加失败");
            }
        }catch (RuntimeException e){
            return Result.error(401,e.getMessage());
        }
        return Result.success(null,null);
    }
    @GetMapping("/getComments")
    public Result getComments(Integer courseId){
        List<ReserveVo> reserveVos = reserveService.getCommentsByCid(courseId);
        Iterator<ReserveVo> iterator = reserveVos.iterator();
        while (iterator.hasNext()){
            ReserveVo reserveVo = iterator.next();
            if (StrUtil.isBlank(reserveVo.getComment())&&ObjectUtil.isEmpty(reserveVo.getScore())){
                iterator.remove();
            }
        }
        return Result.success(null,reserveVos);
    }
    @PostMapping
    public Result comment(@RequestBody Reserve reserve,HttpServletRequest request){
        String token = request.getHeader("token");
        String phone = null;
        try {
            Claims claims = JWTUtil.parseJWT(token);
            phone = claims.getSubject();
        } catch (Exception e) {
            return Result.error(401,"未登录禁止评论");
        }
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        Member member = memberService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(member)){
            return Result.error(401,"登录失效");
        }
        QueryWrapper<Reserve> reserveQueryWrapper = new QueryWrapper<>();
        reserveQueryWrapper.eq("course_id",reserve.getCourseId())
                        .eq("member_id",member.getId());
        Reserve reserve2 = reserveService.getOne(reserveQueryWrapper);
        if (ObjectUtil.isEmpty(reserve2)){
            return Result.error(402,"没有预约该课程，不能评论");
        }
        if (ObjectUtil.isNotEmpty(reserve2.getComment())||ObjectUtil.isNotEmpty(reserve2.getScore())){
            return Result.error(402,"课程已评价，不能重复评价");
        }
        UpdateWrapper<Reserve> reserveUpdateWrapper = new UpdateWrapper<>();
        reserveUpdateWrapper.set("comment",reserve.getComment())
                            .set("comment_time",new Date())
                            .set("score",reserve.getScore())
                                .eq("id",reserve2.getId());
        boolean update = reserveService.update(reserve2, reserveUpdateWrapper);
        if (!update){
            return Result.error(402,"评论异常");
        }
        return Result.success(null,null);
    }
}
